package com.example.wanjialei.service.serviceimpl;

import com.example.wanjialei.mapper.AdvisoryMapper;
import com.example.wanjialei.pojo.Advisory;
import com.example.wanjialei.pojo.AdvisoryExample;
import com.example.wanjialei.pojo.Notice;
import com.example.wanjialei.service.AdvisoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdvisoryImpl implements AdvisoryService {
    @Resource
    private AdvisoryMapper advisoryMapper;

    @Override
    public Map<String, Object> selectAll(Advisory advisory) {
        Map<String, Object> map = new HashMap<>();
        AdvisoryExample advisoryExample = new AdvisoryExample();
        PageHelper.startPage(advisory.getPage(), advisory.getRows());
        List<Advisory> rows = new ArrayList<>();
        if (advisory.getStatus() != null) {

            advisoryExample.or().andStatusEqualTo(advisory.getStatus());
            rows = advisoryMapper.selectByExample(advisoryExample);
        }else{
            rows = advisoryMapper.selectByExample(null);
        }

        PageInfo<Advisory> pageInfo = new PageInfo<>(rows);
        if (rows.size() > 0) {
            map.put("flag", true);
            map.put("message", "查询成功！");
            map.put("total",pageInfo.getTotal());
            map.put("rows",pageInfo.getList());
        } else {
            map.put("flag", false);
            map.put("message", "查询失败！");
            map.put("total",pageInfo.getTotal());
            map.put("rows",pageInfo.getList());
        }
        return map;
    }


    @Override
    public Map<String, Object> modifyById(Advisory advisory) {
        Map<String, Object> map = new HashMap<>();
        int count = advisoryMapper.updateByPrimaryKeySelective(advisory);
        if (count > 0) {
            map.put("flag", true);
            map.put("message", "修改成功！");
            map.put("total", count);
        } else {
            map.put("flag", false);
            map.put("message", "查询失败！");
            map.put("total", count);
        }
        return map;
    }

    @Override
    public Map<String, Object> insert(Advisory advisory) {
        Map<String, Object> map = new HashMap<>();
        int count = advisoryMapper.insert(advisory);
        if (count > 0) {
            map.put("flag", true);
            map.put("message", "插入成功！");
            map.put("total", count);
        } else {
            map.put("flag", false);
            map.put("message", "插入失败！");
            map.put("total", count);
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteById(Advisory advisory) {
        Map<String, Object> map = new HashMap<>();
        int count = advisoryMapper.deleteByPrimaryKey(advisory.getId());
        if (count > 0) {
            map.put("flag", true);
            map.put("message", "删除成功！");
            map.put("total", count);
        } else {
            map.put("flag", false);
            map.put("message", "删除失败！");
            map.put("total", count);
        }
        return map;
    }
}
